package com.yh.service.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.yh.dao.ZhanfengDAO;
import com.yh.dto.UserSearch;
import com.yh.dto.ZhanfengDTO;
import com.yh.entity.Zhanfeng;
import com.yh.exception.CodeException;
import com.yh.service.UserService;
import com.yh.vo.PageVO;
import com.yh.vo.UserVO;
import com.yh.vo.ZhanfengVO;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;

public class UserServiceImpl implements UserService {

	private Logger log = LoggerFactory.getLogger(UserServiceImpl.class);
	private ZhanfengDAO zhanfengDAO = new ZhanfengDAO();

	@Override
	public List<ZhanfengVO> list() {
		List<ZhanfengVO> list = new ArrayList<ZhanfengVO>();
		try {
			List<Zhanfeng> list1 = zhanfengDAO.select(0, 10);
			list1.forEach((entity) -> {
				ZhanfengVO vo = new ZhanfengVO();
				BeanUtil.copyProperties(entity, vo);
				list.add(vo);
			});
		} catch (SQLException e) {
//			e.printStackTrace();
			throw new RuntimeException("获取用户列表失败", e);
		}
		return list;
	}

	@Override
	public void add(ZhanfengDTO zhanfengDTO) {
//		if(StrUtil.hasBlank(zhanfengDTO.getZfName())) {}//效果同下
		if (zhanfengDTO.getZfName() == null || zhanfengDTO.getZfName().trim().equals("")) {
			throw new RuntimeException("用户名不能为空");
		}

//		if(zhanfengDTO.getZfPassword()==null || zhanfengDTO.getZfPassword().equals("")) {}
		if (StrUtil.hasEmpty(zhanfengDTO.getZfPassword())) {
			throw new RuntimeException("密码不能为空");
		}

		// 加密
//		String userPassword = PasswordUtil.encode(user.getUserPassword(), userSalt);

		Zhanfeng zhanfeng1 = new Zhanfeng();
		BeanUtil.copyProperties(zhanfengDTO, zhanfeng1);
		zhanfeng1.setZfTime(new Date());
		zhanfeng1.setZfAge(21);
		try {
			zhanfengDAO.insert(zhanfeng1);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("保存用户数据失败", e);
		}
	}

	@Override
	public int delete(int zfId) {
		try {
			return zhanfengDAO.deleteById(zfId);
		} catch (SQLException e) {
//			e.printStackTrace();
			throw new RuntimeException("删除用户失败", e);
		}
	}

	@Override
	public Zhanfeng Get(Integer zfId) {
		try {
			return zhanfengDAO.selectById(zfId);
		} catch (SQLException e) {
//			e.printStackTrace();
			throw new RuntimeException("查找用户失败", e);
		}
	}

	@Override
	public int update(Zhanfeng user) {
		try {
			return zhanfengDAO.updateById(user);
		} catch (SQLException e) {
//			e.printStackTrace();
			throw new RuntimeException("修改用户失败", e);
		}
	}

	@Override
	public long count() {
		try {
			return zhanfengDAO.count();
		} catch (SQLException e) {
//			e.printStackTrace();
			throw new RuntimeException("查找总数失败", e);
		}
	}

	@Override
	public List<ZhanfengVO> list(int index, int PageSize) {
		List<ZhanfengVO> list = new ArrayList<ZhanfengVO>();
		try {
			List<Zhanfeng> list1 = zhanfengDAO.select(index, PageSize);
			list1.forEach((entity) -> {
				ZhanfengVO vo = new ZhanfengVO();
				BeanUtil.copyProperties(entity, vo);
				list.add(vo);
			});
		} catch (SQLException e) {
//			e.printStackTrace();
			throw new RuntimeException("分页查找失败", e);
		}
		return list;
	}

	@Override
	public PageVO<ZhanfengVO> page(UserSearch search) {
//		try {
//			PageVO<ZhanfengVO> page = new PageVO<ZhanfengVO>(zhanfengDAO.count(), search.getPageNumber(), search.getPageSize());
//			return page.setList(zhanfengDAO.select(index, len))
//		}catch (SQLException e) {
////			e.printStackTrace();
//			throw new RuntimeException("分页查找失败",e);
//		}
		return null;
	}

	@Override
	public List<ZhanfengVO> keylist(String key, int index, int PageSize) {
		List<ZhanfengVO> list = new ArrayList<ZhanfengVO>();
		try {
			List<Zhanfeng> list1 = zhanfengDAO.keySelect(key, index, PageSize);
			list1.forEach((entity) -> {
				ZhanfengVO vo = new ZhanfengVO();
				BeanUtil.copyProperties(entity, vo);
				list.add(vo);
			});
		} catch (SQLException e) {
//			e.printStackTrace();
			throw new RuntimeException("分页查找失败", e);
		}
		return list;
	}

	@Override
	public long keyCount(String key) {
		try {
			return zhanfengDAO.keyCount(key);
		} catch (SQLException e) {
//			e.printStackTrace();
			throw new RuntimeException("查找总数失败", e);
		}
	}

	@Override
	public Zhanfeng login(String userName, String userPwd) {
		Zhanfeng user = new Zhanfeng();
		try {
			user = zhanfengDAO.selectByName(userName);
		}catch (Exception e) {
			throw new CodeException(1,"用户不存在", e);
		}
		if(!user.getZfPassword().equals(userPwd)) {
			throw new CodeException(2, "密码错误");
		}
//		System.out.println(user);
		log.debug("user:{}",user);
		return user;
	}

	@Override
	public List<UserVO> keylistTwo(String key, int index, int PageSize) {
		List<UserVO> list = new ArrayList<UserVO>();
		try {
			list = zhanfengDAO.keySelectTwo(key, index, PageSize);
			
		} catch (SQLException e) {
//			e.printStackTrace();
			throw new RuntimeException("分页查找失败", e);
		}
		return list;
	}

}
